package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.dto.GoodsSalesDTO;
import com.sky.exception.IllegalDataException;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    OrderMapper orderMapper;

    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dates=new ArrayList<>();
        List<BigDecimal> amounts = new ArrayList<>();
        if (begin.isAfter(end)) {
            throw new IllegalDataException(MessageConstant.ILLEGAL_DATE);
        } else if (begin.isEqual(end)) {
            dates.add(begin);
            Double turnover = orderMapper.turnover(begin);
            amounts.add(turnover==null?new BigDecimal(0):new BigDecimal(turnover));
        }else {
            List<BigDecimal> amountsTemp=orderMapper.turnoverStatistics(begin,end);
            List<LocalDate> datesToAmounts=orderMapper.datesToAmounts(begin,end);
            int i=0;
            while (!begin.isAfter(end)){
                dates.add(begin);
                if (i<datesToAmounts.size()&&datesToAmounts.get(i).isEqual(begin))amounts.add(amountsTemp.get(i++));
                else amounts.add(new BigDecimal(0));
                begin=begin.plusDays(1);
            }
        }

        String dateList = StringUtils.join(dates, ",");
        String turnoverList = StringUtils.join(amounts, ",");
        return TurnoverReportVO.builder().dateList(dateList).turnoverList(turnoverList).build();
    }


    public UserReportVO userReportVO(LocalDate begin, LocalDate end) {
        List<LocalDate> dates=new ArrayList<>();
        List<Integer> totalUser = new ArrayList<>();
        List<Integer> newUser = new ArrayList<>();
        if (begin.isAfter(end)) {
            throw new IllegalDataException(MessageConstant.ILLEGAL_DATE);
        }else {
            while (!begin.isAfter(end)){
                dates.add(begin);
                totalUser.add(orderMapper.totalUser(begin));
                newUser.add(orderMapper.newUser(begin));
                begin=begin.plusDays(1);
            }
        }
        String dateList = StringUtils.join(dates, ",");
        String totalUserList = StringUtils.join(totalUser, ",");
        String newUserList = StringUtils.join(newUser, ",");
        return UserReportVO.builder().dateList(dateList).totalUserList(totalUserList).newUserList(newUserList).build();
    }


    public OrderReportVO orderReportVO(LocalDate begin, LocalDate end) {
        List<LocalDate> dates=new ArrayList<>();
        List<Integer> orderCount = new ArrayList<>();
        List<Integer> validOrder = new ArrayList<>();
        if (begin.isAfter(end)) {
            throw new IllegalDataException(MessageConstant.ILLEGAL_DATE);
        }else {
            while (!begin.isAfter(end)){
                dates.add(begin);
                orderCount.add(orderMapper.orderCount(begin));
                validOrder.add(orderMapper.validOrder(begin));
                begin=begin.plusDays(1);
            }
        }

        Integer totalOrderCount=orderMapper.totalOrderCount(end);
        Integer validOrderCount=orderMapper.validOrderCount(end);
        Double orderCompletionRate=validOrderCount*1.0/totalOrderCount;
        String dateList = StringUtils.join(dates, ",");
        String orderCountList = StringUtils.join(orderCount, ",");
        String validOrderCountList = StringUtils.join(validOrder, ",");
        return OrderReportVO.builder().dateList(dateList)
                .orderCountList(orderCountList)
                .validOrderCountList(validOrderCountList)
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }


    public SalesTop10ReportVO salesTop10(LocalDate begin, LocalDate end) {
        List<GoodsSalesDTO> list=orderMapper.salesTop10(begin,end);
        String nameList="",numberList="";
        int count=list.size();
        for (int i=0;i<count;i++) {
            nameList+=list.get(i).getName();
            if(i!=count-1)nameList+=",";
            numberList+=list.get(i).getNumber();
            if(i!=count-1)numberList+=",";
        }
        return SalesTop10ReportVO.builder().nameList(nameList).numberList(numberList).build();
    }
}
